package com.shaoshaohuo.app.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.AppConfig;
import com.shaoshaohuo.app.framework.IViewFramework;
import com.shaoshaohuo.app.ui.LoginActivity;
import com.shaoshaohuo.app.ui.fragment.EcMainFragment;
import com.shaoshaohuo.app.ui.fragment.MyInfoFragment;
import com.shaoshaohuo.app.ui.fragment.NewCartFragment;
import com.shaoshaohuo.app.ui.fragment.Newclassification;
import com.shaoshaohuo.app.utils.LoginUtils;

/**
 * 电商主页导航菜单，管理导航标签点击后显示的fragment
 *
 * @author YinSF
 * @createDate 2015-11-3
 */
public class EcNavigationView extends LinearLayout implements IViewFramework, OnClickListener {
    private RadioButton rbHome;
    private RadioButton rbMarket;
    private RadioButton rbPublish;
    private RadioButton rbCart;
    private RadioButton rbMe;
    private Fragment currentFragment;

    private int currentFragmentViewId = -1;
    private EcMainFragment ecMainFragment;
    private NewCartFragment ecCartFragment;
    private Newclassification newclassification;
    private RadioGroup tab_menu;
    private MyInfoFragment myInfoFragment;
    private NumberView numberview;
    private int i;


    public EcNavigationView(Context context) {
        super(context);
        init();
    }

    public EcNavigationView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.view_navigation_bottom, this);
        init();
    }

    @Override
    public void init() {
        initView();
        initData();
        initListener();
    }

    @Override
    public void initView() {

        rbHome = (RadioButton) findViewById(R.id.rbHome);
        rbMarket = (RadioButton) findViewById(R.id.rbMarket);
//        rbPublish = (RadioButton) findViewById(R.id.rbPublish);
        rbCart = (RadioButton) findViewById(R.id.rbCart);
        rbMe = (RadioButton) findViewById(R.id.rbMe);
        ecMainFragment = EcMainFragment.sharedInstance("");
        ecCartFragment = NewCartFragment.newInstance("2");
//        myInfoFragment =;
        tab_menu = (RadioGroup) findViewById(R.id.tab_menu);
        newclassification = Newclassification.newInstance();
        myInfoFragment = MyInfoFragment.newInstance("");
        numberview = (NumberView) findViewById(R.id.numberview);
        int i = getContext().getResources().getDisplayMetrics().widthPixels / 4 + getContext().getResources().getDisplayMetrics().widthPixels / 8 - BitmapFactory.decodeResource(getContext().getResources(), R.drawable.fenlei_xuanzhonglu).getWidth();
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) numberview.getLayoutParams();
        layoutParams.setMargins(0, 0, i, 0);
    }

    @Override
    public void initData() {
        currentFragmentViewId = rbHome.getId();
    }
   public void curtitem(int index){
       RadioButton childAt = (RadioButton) tab_menu.getChildAt(index);
       childAt.setChecked(true);
       onClick(childAt);
   }
    @Override
    public void initListener() {
        rbHome.setOnClickListener(this);
        rbMarket.setOnClickListener(this);
        rbCart.setOnClickListener(this);
        rbMe.setOnClickListener(this);
//        rbPublish.setOnTouchListener(new View.OnTouchListener() {
//            private boolean down = false;
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                if (event.getAction() == MotionEvent.ACTION_DOWN) {
//                    down = true;
//                } else if (event.getAction() == MotionEvent.ACTION_UP && down) {
//                    getContext().startActivity(new Intent(getContext(), ReleaseActivity.class));
//                    down = false;
//                } else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
//                    down = false;
//                }
//                return true;
//            }
//        });
    }

    @Override
    public void onClick(View v) {
        int viewId = v.getId();


//        if (currentFragmentViewId == viewId) {
//            return;
//        } else {
//            currentFragmentViewId = viewId;
//        }

        switch (viewId) {
            case R.id.rbHome:
                if(currentFragment!=ecMainFragment){
                    i = tab_menu.indexOfChild(v);
                    changefragment(ecMainFragment);
                }

                break;
            case R.id.rbCart:
                if(currentFragment!=ecCartFragment){
                    if(LoginUtils.IsLogining()){
                        i = tab_menu.indexOfChild(v);
                        changefragment(ecCartFragment);
                    }else
                    {
                        ((RadioButton)findViewById(viewId)).setChecked(false);
                        ((RadioButton) tab_menu.getChildAt(i)).setChecked(true);
                        getContext().startActivity(new Intent(getContext(), LoginActivity.class));
                    }
                }

                break;
            case R.id.rbMe:
                if(currentFragment!=myInfoFragment){
                    if(LoginUtils.IsLogining()){
                        changefragment(myInfoFragment);
                        i = tab_menu.indexOfChild(v);
                    }else
                    {
                        ((RadioButton)findViewById(viewId)).setChecked(false);
                        ((RadioButton) tab_menu.getChildAt(i)).setChecked(true);
                        getContext().startActivity(new Intent(getContext(), LoginActivity.class));
                    }

                }

                break;
            case R.id.rbMarket:
                //跳转分类的界面
                if(currentFragment!=newclassification) {
                    i = tab_menu.indexOfChild(v);
                    changefragment(newclassification);


                    break;
                }
            default:
                break;
        }
    }

    public void changeFragment(Fragment newFragment) {
        FragmentActivity activity = (FragmentActivity) getContext();
        FragmentManager fragmentManager = activity.getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (AppConfig.FRAGMENT_CUSTOM_TRANSITION_LEFT_OUT != AppConfig.FRAGMENT_NONE && AppConfig.FRAGMENT_CUSTOM_TRANSITION_RIGHT_IN != AppConfig.FRAGMENT_NONE
                && AppConfig.FRAGMENT_CUSTOM_TRANSITION_LEFT_IN != AppConfig.FRAGMENT_NONE && AppConfig.FRAGMENT_CUSTOM_TRANSITION_RIGHT_OUT != AppConfig.FRAGMENT_NONE) {
            transaction.setCustomAnimations(AppConfig.FRAGMENT_CUSTOM_TRANSITION_RIGHT_IN, AppConfig.FRAGMENT_CUSTOM_TRANSITION_LEFT_OUT,
                    AppConfig.FRAGMENT_CUSTOM_TRANSITION_LEFT_IN, AppConfig.FRAGMENT_CUSTOM_TRANSITION_RIGHT_OUT);
        } else {
            transaction.setTransition(AppConfig.FRAGMENT_TRANSITION);
        }
        if (currentFragment == null) {
            transaction.replace(R.id.frame_ec_main, newFragment, newFragment.getClass().getSimpleName()).commit();
        } else {
            if (currentFragment != newFragment) {
                if (newFragment.isAdded()) {
                    transaction.hide(currentFragment).show(newFragment).commit();
                } else {
                    transaction.hide(currentFragment).add(R.id.frame_ec_main, newFragment, newFragment.getClass().getSimpleName()).show(newFragment).commit();
                }
            }
        }
        currentFragment = newFragment;


    }

    public void changefragment(Fragment newfragment) {
        FragmentActivity activity = (FragmentActivity) getContext();
        FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction();

        if (newfragment != null) {

            if (newfragment.isAdded() && newfragment.isHidden()) {
                Log.e("TAG", "changefragment:-------------- " + "展示");
                fragmentTransaction.show(newfragment);
            } else {
                Log.e("TAG", "changefragment: " + "添加");
                fragmentTransaction.add(R.id.frame_ec_main, newfragment, "fragment").show(newfragment);
            }
        }
        if (currentFragment != null) {
            fragmentTransaction.hide(currentFragment);
        } else {
//            fragmentTransaction.replace(R.id.frame_ec_main, newfragment, newfragment.getClass().getSimpleName()).commit();
            fragmentTransaction.replace(R.id.frame_ec_main, newfragment);
        }
        currentFragment = newfragment;
        fragmentTransaction.commit();
        if (changeListenr != null) {
            changeListenr.change();
        }
    }

    private ChangeListenr changeListenr;

    public interface ChangeListenr {
        void change();
    }

    public void setonchange(ChangeListenr changeListenr) {
        this.changeListenr = changeListenr;
    }

    public void setnum(int num) {
        if (num <= 0) {
            numberview.setVisibility(GONE);
        } else {
            numberview.setVisibility(VISIBLE);
        }
        numberview.setNum(num + "");
    }


}
